﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WHControlLib.Controls.LED
{
    public partial class WHLEDClock : UserControl
    {
        public WHLEDClock()

        {         //设置双缓冲

            //    SetStyle(ControlStyles.OptimizedDoubleBuffer |
            //             ControlStyles.ResizeRedraw |
            //             ControlStyles.AllPaintingInWmPaint, true);

            //    ////背景定义为透明色   
            //    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //    BackColor = Color.Transparent;


            InitializeComponent();
        }
        string  oldtime;

        #region 公共属性定义
        private bool _isTranBackColor=true;
        [Category("A我的"), Description(" 是否钟表背景透明，默认，true"), Browsable(true)]
        public bool IsTranBackColor
        {
            get { return _isTranBackColor; }
            set
            {
                _isTranBackColor = value;
                Invalidate();
            }
        }


        private bool _isShowBorder;
        [Category("A我的"), Description(" 是否显示外形的边框的描边，默认，false"), Browsable(true)]
        public bool IsShowBorder
        {
            get { return _isShowBorder; }
            set {
                whled1.IsShowBorder = value; 
                _isShowBorder = value;
                Invalidate();
            }
        }

        private Color _clockColor=Color.Black;
        [Category("A我的"), Description("钟表的颜色，默认，黑色"), Browsable(true)]
        public Color ClockColor
        {
            get { return _clockColor; }
            set { whled1.LEDLineColor = value;
                
                _clockColor = value; Invalidate();
            }
        }

        private Color _clockColorBorderColor = Color.Blue;
        [Category("A我的"), Description("如果允许描边钟表的描边颜色，默认，蓝色"), Browsable(true)]
        public Color ClockColorBorderColor
        {
            get { return _clockColorBorderColor; }
            set {
                whled1.BorderColor= value;
                _clockColorBorderColor = value; Invalidate(); }
        }
        private int _clockBorderWidth=1;
        [Category("A我的"), Description("如果允许描边钟表的描边宽度，默认，1，不要太大"), Browsable(true)]
        public int ClockBorderWidth
        {
            get { return _clockBorderWidth; }
            set {
                whled1.BorderWidth = value;
                
                _clockBorderWidth = value; Invalidate();
            }
        }

        private int _clockLEDWidth=9;
        [Category("A我的"), Description("钟表或日历LED每段的宽度，默认，9"), Browsable(true)]
        public int ClockLEDWidth
        {
            get { return _clockLEDWidth; }
            set
            {
                if (value <= 0)
                {
                    whled1.LEDLineWidth = 1;
                    _clockLEDWidth = 1;
                }
                else
                {
                  whled1.LEDLineWidth = value;
                _clockLEDWidth = value;
                }
                Invalidate();
            }
        }
        private int _clockSecondSpeed=500;
        [Category("A我的"), Description("钟表秒表读取时间的采样率单位毫秒，默认，500 半秒一次，没有特殊需求不要改动"), Browsable(false)]
        public int ClockSecondSpeed
        {
            get { return _clockSecondSpeed; }
            set
            {
                if (value<=10)
                {
                    timer1.Interval = 10;
                    _clockSecondSpeed = 10;
                }
                else
                {
                    timer1.Interval = value;

                    _clockSecondSpeed = value;
                }
               }
        }
        private bool  _StartClock;
        [Category("A我的"), Description("  开启钟表计时，默认，关"), Browsable(true)]
        public bool StartClock
        {
            get { return _StartClock; }
            set {
                if (value)
                {
                    oldtime = DateTime.Now.ToString("HH:mm:ss");
                    timer1.Start();
                }
                else {
                    oldtime = "";
                  timer1.Stop();

                } 
                  
               
                _StartClock = value; }
        }

        public enum ClockMode
        {
            NoSecond,ShowSecond,OnlyDate
        }

        private ClockMode _myClockMode;
        [Category("A我的"), Description("钟表或日历 模式的切换，默认，无秒表时钟"), Browsable(true)]
        public ClockMode MyClockMode
        {
            get { return _myClockMode; }
            set { _myClockMode = value; Invalidate(); }
        }

        #endregion
        private void WHLEDtimer_Load(object sender, EventArgs e)
        {
            whled1.Height = Height;
            whled1.Width = Width;
            whled1.Top = 0;
            whled1.Left = 0;
            if (IsTranBackColor)
            {
            BackColor = Color.Transparent;
            }
          
            StartClock = true;        
    
          
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        

            this.Invoke(new MethodInvoker(() => {
              
             switch (MyClockMode)
            {
                case ClockMode.NoSecond:
                        if (oldtime== DateTime.Now.ToString("HH:mm:ss"))
                        {
                        whled1.LEDStrValue = DateTime.Now.ToString("HH!mm");
                        }
                        else
                        {
                            whled1.LEDStrValue = DateTime.Now.ToString("HH:mm");
                            oldtime = DateTime.Now.ToString("HH:mm:ss");
                        }
                       
                        break;
                case ClockMode.ShowSecond:
                        whled1.LEDStrValue = DateTime.Now.ToString("HH:mm:ss");
                        break;
                case ClockMode.OnlyDate:
                        whled1.LEDStrValue = DateTime.Now.ToString("yyyy-MM-dd");
                        break;
                default:
                        whled1.LEDStrValue = DateTime.Now.ToString("HH:mm:ss");
                        break;
            }



                }));
                   
        
       
              
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            whled1.Height = Height;
            whled1.Width = Width;
            whled1.Top = 0;
            whled1.Left = 0;
            base.OnSizeChanged(e);
        }


        /////////////////////////////////////////////////////////
    }
}
